package main.java.controller;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Modifier;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import main.java.DAO.IEnrollDAO;
import main.java.model.CommandBean;
import main.java.model.CommandTransmitter;
import main.java.model.EnrollContentModel;
import main.java.model.EnrollMetadataModel;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/test")
public class TestController {
	@Autowired
	@Qualifier("IEnrollDAO")
	IEnrollDAO enrollDAO;
	
	
	@RequestMapping(method=RequestMethod.POST, value="/test/")
	public String test(@RequestBody String json,
			ModelMap modelMap
			) throws UnsupportedEncodingException		{
		//-------- 毕设服务器接收端代码 --------
//		GsonBuilder builder = new GsonBuilder();
//		builder.excludeFieldsWithModifiers(Modifier.TRANSIENT);
//		Gson gson = builder.create();
////		List<CommandBean> after = gson.fromJson(list, new TypeToken<List<CommandBean>>(){}.getType());
//		CommandTransmitter transmitter = gson.fromJson(list, CommandTransmitter.class);
//		System.out.println("/**********************/");
//		System.out.println(transmitter);
		
		//--------post json test--------
		String ripeJson = URLDecoder.decode(json, "UTF-8");
		Pattern pattern = Pattern.compile("\\{.*\\}");
		Matcher matcher = pattern.matcher(ripeJson);
		if(matcher.find())		{
			ripeJson = matcher.group();
		}
		System.out.println(ripeJson);
		modelMap.put("state", ripeJson);
		return "main/resources/JSP/state";
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/test2/")
	public String test()		{
//		EnrollMetadataModel enrollMetadataModel = new EnrollMetadataModel();
//		enrollMetadataModel.setIsOpen(0);
//		enrollMetadataModel.setMetadata("456美国()\\\"");
//		enrollMetadataModel.setName("test2");
//		enrollDAO.insert(enrollMetadataModel);
		
//		List<EnrollMetadataModel> list = enrollDAO.getAll();
//		for(EnrollMetadataModel enrollMetadataModel : list)			{
//			System.out.println(enrollMetadataModel);
//		}
		
//		EnrollContentModel enrollContentModel = new EnrollContentModel();
//		enrollContentModel.setContent("qqq1");
//		enrollContentModel.setMetadata_id("3");
//		enrollDAO.insertContent(enrollContentModel);
//		enrollContentModel.setContent("qqq2");
//		enrollContentModel.setMetadata_id("3");
//		enrollDAO.insertContent(enrollContentModel);
//		enrollContentModel.setContent("www1");
//		enrollContentModel.setMetadata_id("4");
//		enrollDAO.insertContent(enrollContentModel);
//		enrollContentModel.setContent("www2");
//		enrollContentModel.setMetadata_id("4");
//		enrollDAO.insertContent(enrollContentModel);
		
//		List<EnrollContentModel> list = enrollDAO.getContentByEnrollId("3");
//		for(EnrollContentModel enrollContentModel : list)		{
//			System.out.println(enrollContentModel);
//		}
		
		return "";
	}
}
